package com.pharmacy.controller;

import com.pharmacy.dto.AddOrdersDto;
import com.pharmacy.dto.OrdersPageSelectDto;
import com.pharmacy.dto.UpdateOrdersDto;
import com.pharmacy.entity.WeeklyData;
import com.pharmacy.result.PageResult;
import com.pharmacy.result.Result;
import com.pharmacy.service.OrdersService;
import com.pharmacy.vo.TodayDate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


@RestController
@RequestMapping("/admin/orders")
@Slf4j
@Tag(name = "订单管理")
@CrossOrigin
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    @Operation(summary = "增加订单")
    @PostMapping("/addOrders")
    public Result addOrders(@RequestBody AddOrdersDto addOrdersDto){
        ordersService.addOrders(addOrdersDto);
        return Result.success();
    }

    @Operation(summary = "分页查询")
    @GetMapping("/pageSelect")
    public Result<PageResult> pageSelect(OrdersPageSelectDto ordersPageSelectDto){
        log.info("分页查询订单{}",ordersPageSelectDto);
        PageResult pageResult = ordersService.pageSelect(ordersPageSelectDto);
        return Result.success(pageResult);
    }

    @Operation(summary = "删除订单")
    @DeleteMapping("/delectOrder/{orderId}")
    public Result delectOrder(@PathVariable Integer orderId){
        log.info("删除订单id：{}",orderId);
        ordersService.deleteOrders(orderId);

        return Result.success();
    }

    @Operation(summary = "修改订单")
    @PutMapping("/updateOrder")
    public Result updateOrder(@RequestBody UpdateOrdersDto updateOrdersDto){

        if(updateOrdersDto.getMedicine() == null){
            updateOrdersDto.setMedicine("999感冒灵*1, 阿莫西林*1");
        }
        if (updateOrdersDto.getPrice() == null){
            updateOrdersDto.setPrice(BigDecimal.valueOf(39.8));
        }
        ordersService.updateOrders(updateOrdersDto);
        return Result.success();
    }

    /**
     * 查询当天的订单
     * @param date
     * @return
     */
    @GetMapping("/todayDate/{date}")
    public Result getTodayDate(@PathVariable String date) {
        // 解析日期字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parsedDate = LocalDate.parse(date, formatter);
        TodayDate todayDate = ordersService.selectTodayDate(parsedDate);
        return Result.success(todayDate);
    }


    /**
     * 查询本周的订单
     * @return
     */
    @GetMapping("/week")
    public Result<WeeklyData> getWeeklyData(){
        WeeklyData weeklyData = ordersService.weeklyData();
        return Result.success(weeklyData);
    }


    @PutMapping("/updateWeeklyData/{data}")
    public Result updateWeeklyData(@PathVariable String date){
        ordersService.updateWeeklyData(date);
        return Result.success();
    }
}
